package com.android.cookbook.activity;

import java.io.IOException;

import com.android.cookbook.R;
import com.android.cookbook.R.id;
import com.android.cookbook.R.layout;
import com.android.cookbook.bean.Recipe;
import com.android.cookbook.db.BaseDatosHelper;
import com.android.cookbook.listener.RecipeCreateListener;
import com.android.cookbook.listener.RecipeHomeListener;
import com.android.cookbook.listener.RecipeSearchListener;
import com.android.cookbook.util.Constant;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class RecipeCreate extends Activity {
	
	private EditText edit_name;
	private EditText edit_description;
	private EditText edit_ingredients;
	private EditText edit_direction;
	BaseDatosHelper myDBHelper;
	String strTypeCreate = "";
	int     recipeId;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item_create);
        
        edit_name = (EditText) findViewById(R.id.edit_name);
        edit_description = (EditText) findViewById(R.id.edit_description);
        edit_ingredients = (EditText) findViewById(R.id.edit_ingredients);
        edit_direction = (EditText) findViewById(R.id.edit_direction);
        
        createDB();
        
        Bundle extras = getIntent().getExtras();
        if (extras!=null){
        	strTypeCreate = extras.getString(Constant.TYPE_CREATE); 
        	if ("EDIT".equals(strTypeCreate)){
        		recipeId = extras.getInt("_id");
        		Recipe myRecipe = getRecipe(recipeId);
        		edit_name.setText(myRecipe.getName());
        		edit_description.setText(myRecipe.getDescription());
        		edit_ingredients.setText(myRecipe.getIngredientes());
        		edit_direction.setText(myRecipe.getDirection());
        	}
        }
        
        Button create = (Button) findViewById(R.id.create);
        create.setOnClickListener(new View.OnClickListener() {             
        	public void onClick(View view) {
        		if (validateForm()){
        			if ("EDIT".equals(strTypeCreate)){
        				editRecipe();
        			}else{
        				createRecipe();
        			}
        			Bundle bundle = new Bundle();                 
        			bundle.putString(Constant.TYPE_ID,Constant.TYPE_MY_RECIPE);                   
        			bundle.putString(Constant.TYPE_SEARCH,Constant.LIST_BY_ID);   

        			Intent myIntent = new Intent(view.getContext(),ListDriver.class);
        			myIntent.putExtras(bundle);  
        			RecipeCreate.this.finish();
        			startActivityForResult(myIntent, 0);
        		}
        	}          
        }); 

        Button exit = (Button) findViewById(R.id.salir);
        exit.setOnClickListener(new RecipeHomeListener(this));
        
        ImageView search = (ImageView)findViewById(R.id.create_search);
        search.setOnClickListener(new RecipeSearchListener(this));
        
        ImageView home = (ImageView) findViewById(R.id.home);
        home.setOnClickListener(new RecipeHomeListener(this));
        
    }
    
	public void createDB() {         
		myDBHelper = new BaseDatosHelper(this);         
		try {             
			myDBHelper.createDataBase();         
		} catch (IOException ioe) {             
			throw new Error("Unable to create database");         
		}     
	}    

	public Recipe getRecipe(int idDetail) {         
		myDBHelper.openDataBase();         
		Recipe recipe = myDBHelper.getRecipe(idDetail);         
		myDBHelper.close();         
		return recipe;     
	}
	
	public void createRecipe() {         
		myDBHelper.openDataBase();         
		myDBHelper.createRecipe(edit_name.getText().toString(),
				  				edit_description.getText().toString(),
				  				edit_ingredients.getText().toString(),
				  				edit_direction.getText().toString());       
		myDBHelper.close();         
		return;     
	}

	public void editRecipe() {         
		myDBHelper.openDataBase();         
		myDBHelper.updateRecipe(recipeId,
								edit_name.getText().toString(),
								edit_description.getText().toString(),
								edit_ingredients.getText().toString(),
								edit_direction.getText().toString());       
		myDBHelper.close();         
		return;     
	}
	
	public boolean validateForm(){
		if("".equals(edit_name.getText().toString())){
			AlertDialog.Builder builder = new AlertDialog.Builder(this); 
	    	builder.setMessage(R.string.validate_name); 
	    	builder.setPositiveButton("OK", null);
	    	AlertDialog alert = builder.create();
	    	alert.show();
			
			return false;
		}
		if("".equals(edit_ingredients.getText().toString())){
			AlertDialog.Builder builder = new AlertDialog.Builder(this); 
	    	builder.setMessage(R.string.validate_ingredients); 
	    	builder.setPositiveButton("OK", null);
	    	AlertDialog alert = builder.create();
	    	alert.show();
			
			return false;
		}
		if("".equals(edit_direction.getText().toString())){
			AlertDialog.Builder builder = new AlertDialog.Builder(this); 
	    	builder.setMessage(R.string.validate_direction); 
	    	builder.setPositiveButton("OK", null);
	    	AlertDialog alert = builder.create();
	    	alert.show();
			
			return false;
		}
		return true;
	}

}
